package com.hhw.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.User;
import com.hhw.util.DBUtil;




public class UserDao  extends BaseDao {
			
	/*
	 * 查询全部用户方法
	 */
	
	public List<User> findUser(){
				List<User> list=new ArrayList<>();
				try{
					connection = DBUtil.getConnection();
					preparedStatement=connection.prepareStatement("select * from user ");
					resultSet=preparedStatement.executeQuery();
					User r= null;
					while(resultSet.next()){
						 r= new User();
						 r.setUser_id(resultSet.getInt("user_id"));
						 r.setUser_name(resultSet.getString("user_name"));
						 r.setUser_password(resultSet.getString("user_password"));
						 r.setUser_sex(resultSet.getString("user_sex"));
						 r.setUser_phone(resultSet.getString("user_phone"));
						 r.setUser_add(resultSet.getString("user_add"));
						 list.add(r);
					}
					return list;
					}catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						return null;
				    }
			}
				
			/*
			 * 判断用户是否存在<根据name,password>
			*/

			public boolean isExist(User r){
				boolean flag = false;
				try {
					int count = 0;
					connection = DBUtil.getConnection();
					String sql = "select count(*) as count from user where user_name = ? and user_password = ?";
					preparedStatement = connection.prepareStatement(sql);
					
					preparedStatement.setString(1,r.getUser_name());
					preparedStatement.setString(2, r.getUser_password());
					
					resultSet = preparedStatement.executeQuery();
					while (resultSet.next()) {
						
						count = resultSet.getInt("count");
					}
					if (count != 0) {
						flag = true;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					closed();
				}
				return flag;
			}
			
			
			/*
			 * 查询用户根据<name>
			  */
			
			
			public List<User> findAllUser(String user_name){
				List<User> list = new ArrayList<>();
				try{
					connection = DBUtil.getConnection();
					preparedStatement=connection.prepareStatement("select * from user where user_name=?");
					preparedStatement.setString(2, user_name);
					resultSet=preparedStatement.executeQuery();
					User r= null;
					while(resultSet.next()){
						 r= new User();
						 r.setUser_id(resultSet.getInt("user_id"));
						 r.setUser_name(resultSet.getString("user_name"));
						 r.setUser_password(resultSet.getString("user_password"));
						 r.setUser_sex(resultSet.getString("user_sex"));
						 r.setUser_phone(resultSet.getString("user_phone"));
						 r.setUser_add(resultSet.getString("user_add"));
						 list.add(r);
					}
					return list;
					}catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						return null;
				    }
			}
			

			
			}

